//
//  AppTools.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
@implementation AppTools


+ (void)getDataWithURL:(NSString *)url
                 Block:(AppToolsDataBlock)block{
    //判断网络
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
//    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    
    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlEncode parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        //block回调
        block (responseObject);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
 
    
}


+ (void)getImageWithURL:(NSString *)url
                  Block:(AppToolsImageBlock)block{
    
    NSURL *url1 = [NSURL URLWithString:url];
    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url1];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task= [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        block (data);
        
        
    }];
    
    [task resume];
    
    
    
    
    
    
    
}




@end
